package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.ApArticleConfigService;
import com.heima.common.constants.WmNewsMessageConstants;
import org.apache.avro.data.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 文章上下架监听类
 */
@Component
public class DownOrUpListener {
    @Autowired
    private ApArticleConfigService apArticleConfigService;
    /**
     * 监听队列的方法
     */
    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void getMessage(String message){
        //1.类型转换
        Map map = JSON.parseObject(message, Map.class);
        //2.获取文章id和enable值
        Long articleId = (Long) map.get("articleId");
        /**
         * enable=0表示下架,enable=1表示上架
         */
        Integer enable = (Integer) map.get("enable");

        //3.同步修改ap_article_config表中的is_down的值
        apArticleConfigService.downOrUpArticle(articleId,enable);
    }
}
